package com.zhu.service.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.commons.utils.R;
import com.zhu.service.cms.FeignService.AliyunFeign;
import com.zhu.service.cms.pojo.CmsAd;
import com.zhu.service.cms.mapper.CmsAdMapper;
import com.zhu.service.cms.service.ICmsAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.service.cms.vo.CmsAdVo;
import com.zhu.service.edu.feign.OssFeignService;
import com.zhu.service.edu.pojo.vo.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-08-14
 */
@Service
@Slf4j
public class CmsAdServiceImpl extends ServiceImpl<CmsAdMapper, CmsAd> implements ICmsAdService {
@Autowired
private CmsAdMapper cmsAdMapper;
@Autowired
private   AliyunFeign aliyunFeign;

    @Override
    public IPage<CmsAdVo> selectPage(Page<CmsAdVo> pages) {
        return null;
    }

    /**
     * 查询推荐列表
     * @param currentPage
     * @param size
     * @param cmsAdVo
     * @return
     */
    @Override
    public PageBean selectPages(Long currentPage, Long size, CmsAdVo cmsAdVo) {
        Page<CmsAdVo> page=new Page<>(currentPage,size);
        IPage<CmsAdVo> IPage=cmsAdMapper.selectPages(page);
        PageBean pageBean=new PageBean(IPage.getTotal(),IPage.getRecords());
        return pageBean;
    }

    /**
     * 根据id删除推荐列表图片
     * @param id
     */
    @Override
    public Boolean removeImageById(String id) {
        CmsAd cmsAd = cmsAdMapper.selectById(id);
        String imageUrl = cmsAd.getImageUrl();
        if(StringUtils.isNotEmpty(imageUrl)){
            R r = aliyunFeign.DeleteFile(imageUrl);
            log.info("删除图片成功！:{}",imageUrl);
            return r.getSuccess();
        }
        return false;
    }

    /**
     * 根据id查询广告列表
     * @param id
     * @return
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
//    @Cacheable(value = "index",key="'selectById'")   不好使不知道为什么
    public List<CmsAd> selectById(String id) {
        /**
         * 当缓存中没有数据的时候存入缓存
         */
        List<CmsAd> adList= (List<CmsAd>) redisTemplate.opsForValue().get("adList");
        if(adList!=null){
            log.info("走了缓存");
            return adList;
        }

        LambdaQueryWrapper<CmsAd> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(CmsAd::getSort).orderByAsc(CmsAd::getId);
        queryWrapper.eq(CmsAd::getTypeId,id);
        List<CmsAd> adList2 = cmsAdMapper.selectList(queryWrapper);
        redisTemplate.opsForValue().set("adList",adList2);
        log.info("走了数据库");
        return adList2;
    }
}
